package Lab9;

//import Lab9.MergeSort;

import java.util.Arrays;
import java.util.Comparator;

public class InsertionSort {
    public static <E extends Comparable<E>> void insertionSort(E[] list) {
        insertionSort(list, Comparator.naturalOrder()); //Comparable::compareTo); //(e1, e2) -> e1.compareTo(e2)
    }

    public static <E> void insertionSort(E[] list, Comparator<? super E> comparator) {
        for (int i = 1; i < list.length; i++) {
            E temp = list[i];
            int j;
            for (j = i - 1; j >= 0 && comparator.compare(temp, list[j])<0; j--){
                list[j+1]=list[j];
            }
            list[j+1] = temp;
        }
    }


    public static void main(String[] args){
        Integer[] ls = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};

        insertionSort(ls, Comparator.reverseOrder()); //(e1, e2) -> e2.compareTo(e1)
        System.out.println(Arrays.toString(ls));
        insertionSort(ls);
        System.out.println(Arrays.toString(ls));


    }
}
